<template>
	<view class="resultTask">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" title="内转钢坯" fixed="true" left-icon="left" left-text="返回" @clickLeft="back"></uni-nav-bar>
		<view class="resultTask_top" v-if="transOrderId == null">
			<view class="" style="margin-top: 10rpx; margin-bottom: 10rpx"></view>
			<uni-section title="钢坯合同" type="line" title-font-size="20px">
				<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0" class="dilSearch"></dil-search>
				<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
			</uni-section>
		</view>
		<view class="resultTask_middle" v-show="transOrderId == 1">
			<uni-section title="装货信息" type="line" titleFontSize="20px">
				<uni-card margin="10rpx" padding="4rpx">
					<dil-Form :formId="788" v-model="formValue" labelPosition="left" ref="form"></dil-Form>
					<div style="display: flex; justify-content: center; align-items: center">
						<button type="danger" class="button" @click="cancelMask">取消</button>
						<button type="primary" class="button" @click="onclickConfirm">装货确认</button>
					</div>
				</uni-card>
			</uni-section>
		</view>
		<view class="resultTask_middle" v-show="transOrderId == 2">
			<uni-section title="装货信息" type="line" titleFontSize="20px">
				<uni-card margin="10rpx" padding="4rpx">
					<dil-Form :formId="789" v-model="formValue" labelPosition="left" ref="form"></dil-Form>
					<div style="display: flex; justify-content: center; align-items: center">
						<button type="danger" class="button" @click="cancelMask">取消</button>
						<button type="primary" class="button" @click="onclickConfirm">装货确认</button>
					</div>
				</uni-card>
			</uni-section>
		</view>
	</view>
</template>

<script>
import nfc from '@/utils/nfc.js';
export default {
	data() {
		return {
			addressMap: {},
			resultStartTime: null,
			formValue: {},
			options: {
				requestUrl: '/api/v1/ams/getproductionRequirementList?apiId=719',
				requestQuery: {
					businessType: '129988'
				},
				cardSlotList: [
					{
						label: '刷ic卡装车',
						icon: 'wlIcon-zhuangcherenwu',
						emitEvent: 'excuteMakeIc'
					},
					{
						label: '扫码装车',
						icon: 'scan',
						emitEvent: 'excuteMake'
					}
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			},
			transOrderId: null
		};
	},
	onShow() {
		uni.$emit('onShow', 1);
		// 以点进来的时间作为开始时间;
		this.resultStartTime = new Date().getTime();
	},
	mounted() {
		this.getOrderSectionDebounce = this.Tool.debounce((data) => {
			this.getOrderSection(data);
		}, 500);
	},
	computed: {},
	methods: {
		excuteMakeIc(item) {
			this.transOrderId = 1;
			this.formValue = { ...this.formValue, ...item };
			this.onNfc();
		},
		excuteMake(item) {
			this.formValue = { ...this.formValue, ...item };
			this.onScan();
		},
		closeNfc() {
			nfc.closeNFC();
		},
		listenNfc() {
			nfc.initNFC();
		},
		runNfc(callback) {
			nfc.readNFC(callback);
		},
		async onNfc() {
			uni.showToast({
				duration: 3000,
				title: '请根据提示操作',
				icon: 'none'
			});
			await this.listenNfc();
			await this.runNfc((data) => {
				if (data.includes('00000000000')) {
					uni.showToast({
						duration: 2000,
						title: '识别失败,请重新识别',
						icon: 'none'
					});
					return;
				}
				this.formValue.icCardNo = data;
			});
		},
		cancelMask() {
			this.transOrderId = null;
			this.transOrderNumber = null;
			this.formValue = {};
			this.getOrderSection();
		},
		arrow() {
			uni.navigateTo({
				url: '/pages/transExecute/loadResult'
			});
		},
		onSearch(item) {
			item.businessType = 129988;
			this.getOrderSectionDebounce(item);
		},
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		async getOrderSection(data = {}) {
			data.businessType = 129988;
			this.options.requestQuery = { ...data };
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},

		//扫描事件
		onScan() {
			let that = this;
			that.$commonScan((data) => {
				let scanResult = data.scanResult;
				if (!scanResult.includes('YSDD') && scanResult.length != '20') {
					uni.showToast({
						title: '识别信息不符合规则,请重试',
						icon: 'none',
						duration: 2000
					});
				} else {
					that.transOrderNumber = scanResult;
					that.getTransOrderInFoByNo({
						transOrderNumber: that.transOrderNumber
					});
				}
			});
		},
		async getTransOrderInFoByNo(data) {
			const res = await this.$myRequest({
				url: '/api/v1/uc/getTransOrderInFoByNo',
				data: data
			});
			if (res.data.status == 'succeed') {
				if (res.data.data.length > 0) {
					this.formValue = { ...this.formValue, ...res.data.data[0] };
					this.transOrderId = 2;
				} else {
					uni.showToast({
						duration: 2000,
						title: '读取运单失败',
						icon: 'error'
					});
				}
			} else {
				uni.showToast({
					duration: 2000,
					title: '读取运单失败',
					icon: 'error'
				});
			}
		},
		//确认事件
		onclickConfirm() {
			let that = this;
			this.$refs['form'].validateForm((data) => {
				if (data.flag) {
					uni.showModal({
						cancelText: '取消',
						confirmText: '确认',
						title: '提示',
						content: '确定是否装货完成',
						success: (res) => {
							if (res.confirm) {
								console.log('确认装货');
								that.confirmExecute();
							} else if (res.cancel) {
								uni.showToast({
									title: '用户取消操作！',
									icon: 'none',
									duration: 3000
								});
							}
						}
					});
				}
			});
		},
		async confirmExecute() {
			let data = this.formValue;
			data.transOrderNumber = this.options.rowDataList[0].transOrderNumber;
			data.loaderName = uni.getStorageSync('userName');
			//首先看哪些列需要传图片ssss
			const res = await this.$myRequest({
				url: '/api/v1/tms/dispatchTask',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});

				setTimeout(() => {
					this.cancelMask();
				}, 2000);
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});
			}
		}

		//vuex
	}
};
</script>

<style lang="scss" scoped>
.resultTask {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	/deep/ .segmentControl {
		// flex: auto !important;
		margin-top: 1rpx;
		.segmented-control__item--button {
			border-style: none;
			border-top-width: 0px;
			border-bottom-width: 0px;
			border-right-width: 0px;
			border-left-width: 0;
		}
		.segmented-control__item--button--first {
			border-left-width: 0px;
			border-top-left-radius: 0px;
			border-bottom-left-radius: 0px;
		}
		.segmented-control__item--button--last {
			border-top-right-radius: 0px;
			border-bottom-right-radius: 0px;
		}
	}
	&_top {
		width: 100vw;
		height: 100%;
		.uni-section {
			width: 100vw;
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				.dilSearch {
					// flex: none;
					width: 100%;
				}
				.noTip {
					width: 100vw;
					height: 10vh;
					display: flex;
					justify-content: center;
					align-items: center;
					font-weight: 700;
					font-size: 40rpx;
					color: #000;
				}
			}
		}
	}
	.resultTask_top_result {
		margin-top: 20rpx;
		.scroll-Y {
			height: 90vh;
		}
	}
	&_main {
		margin-top: 20rpx;
	}
	&_middle {
		margin-top: 20rpx;
		.uni-section {
			width: 100vw;
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
			}
			/deep/.uni-card {
				border-radius: 40rpx;
				.button {
					border-radius: 40rpx;
				}
			}
		}
	}
	&_floot {
		width: 100vw;
		background-color: #fff;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
	}
}
</style>
ss
